package com.shijun.customview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.shijun.bean.DataBean;
import com.shijun.bean.FriendBean;
import com.shijun.pullrecyclerviewdemo.R;
import com.shijun.utils.Utils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhushijun on 2018/1/1.
 */

public class CustomAutoLineFeedView extends LinearLayout {
    private int maxWidth;
    private Context context;
    private List<DataBean.UserBean> data;
    private LayoutParams rowLayoutParams;
    private LayoutParams itemLayoutParams;
    private int rowInterval;
    private int colInterval;
    private LayoutInflater inflater;
    public CustomAutoLineFeedView(Context context) {
        super(context);
    }
    private OnClickItemListener listener;

    public CustomAutoLineFeedView(Context context, List<DataBean.UserBean> data, int maxWidth) {
        super(context);
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = data;
        if(this.data==null){
            this.data = new ArrayList<>();
        }
        this.maxWidth = maxWidth;
        setOrientation(LinearLayout.VERTICAL);
        rowInterval = getPxById(R.dimen.y40);
        colInterval = getPxById(R.dimen.y20);
        rowLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        rowLayoutParams.setMargins(0,rowInterval/2,0,rowInterval/2);
        itemLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        itemLayoutParams.setMargins(colInterval/2,0,colInterval/2,0);
        initView();
    }

    private void initView() {
        removeAllViews();
        LinearLayout rowLayout = new LinearLayout(context);
        rowLayout.setLayoutParams(rowLayoutParams);
        int residue = maxWidth;
        for(int i=0;i<data.size();i++){
            final int position = i;
            LinearLayout viewItem = (LinearLayout) inflater.inflate(R.layout.inflater_autolinefeed_item, null);
            viewItem.setLayoutParams(itemLayoutParams);
            viewItem.setOrientation(LinearLayout.HORIZONTAL);
            TextView textView = (TextView) viewItem.findViewById(R.id.inflater_autolinenfeed_item_content_tv);
            textView.setText(data.get(position).getUserName());
            viewItem.measure(0, 0);
            if (residue < viewItem.getMeasuredWidth()) {
                addView(rowLayout);
                rowLayout = new LinearLayout(context);
                rowLayout.setLayoutParams(rowLayoutParams);
                residue = maxWidth;
            }

            rowLayout.addView(viewItem);
            residue = residue-viewItem.getMeasuredWidth()-colInterval;

            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onClickItem(view,position);
                }
            });
        }

        if(residue!=maxWidth){
            addView(rowLayout);
        }
    }





    public void  notifyDataSetChanged(List<DataBean.UserBean> data){
        this.data.clear();
        this.data.addAll(data);
        initView();
    }

    private int getPxById(int id) {
        return context.getResources().getDimensionPixelSize(id);
    }

    public void setOnClickItemListener(OnClickItemListener listener) {
        this.listener = listener;
    }

    public interface OnClickItemListener{
        void onClickItem(View view,int position);
    }
}